package com.etc.common;

import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.triggers.CronTriggerImpl;

import java.text.ParseException;

import static org.quartz.JobBuilder.newJob;

/**
 * @author Chen
 * @since 2021/1/15 - 16:07
 */
public class QuartzUtil {

	public static void schedule(Class <? extends Job> clazz, String cron, String triggerName) throws ParseException, SchedulerException {
		JobDetail jobDetail = newJob(clazz).build();
		System.out.println("jobDetail = " + jobDetail);
		CronTriggerImpl trigger = new CronTriggerImpl();
		trigger.setCronExpression(cron);
		trigger.setName(triggerName);
		System.out.println("trigger = " + trigger);
		Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
		System.out.println("scheduler = " + scheduler);
		scheduler.scheduleJob(jobDetail ,trigger);
		scheduler.start();
	}

}